package question;

import utils.TokenUtils;

import java.util.List;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.PreparedStatement;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-6-16
 * Time: 21:25:54
 */
public class Question {

  public static void addQuestionList(Connection con, List<QuestionInfo> list) throws SQLException {
    String sql = "INSERT INTO question VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    PreparedStatement ps = con.prepareStatement(sql);

    for (QuestionInfo info : list) {
      ps.setInt(1, info.getCid());
      ps.setInt(2, info.getTx());
      ps.setString(3, info.getContent());
      ps.setString(4, info.getAnswer());
      ps.setInt(5, info.getImportant());
      ps.setInt(6, info.getComplexity());
      ps.setInt(7, info.getType());
      ps.setLong(8, info.getPickTime());
      ps.setLong(9, info.getRightTime());
      ps.executeUpdate();
    }
  }

  public static void delQuestion(Connection con, int[] ydIds) throws SQLException {
    if (ydIds == null || ydIds.length == 0) return;
    String sql = "DELETE FROM question WHERE cid IN (" + TokenUtils.getTokenString(ydIds, ",") + ")";
    con.createStatement().executeUpdate(sql);
  }

  public static long getQuestionSum(Connection con, int cid) throws SQLException {
    String sql = "SELECT count(*) AS sum FROM question, classify" +
            " WHERE question.cid = classify.cid AND classify.parentID=" + cid;
    ResultSet rs = con.createStatement().executeQuery(sql);
    return rs.next() ? rs.getLong("sum") : 0;
  }

  public static List<QuestionInfo> getQuestionList(Connection con, int cid, long position, long sum) throws SQLException {
    String sql = "SELECT * FROM question, classify" +
            " WHERE question.cid = classify.cid AND classify.parentID = ? LIMIT ?, ?";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setInt(1, cid);
    ps.setLong(2, position);
    ps.setLong(3, sum);
    ResultSet rs = ps.executeQuery();

    List<QuestionInfo> list = new ArrayList<QuestionInfo>();
    while (rs.next()) list.add(rs2Info(rs));
    return list;
  }

  public static List<QuestionInfo> getQuestionList(Connection con, int kmID) throws SQLException {
    List<ClassifyInfo> ydList = Classify.getYdList(con, kmID);
    String ydids = "";
    for (ClassifyInfo info : ydList) ydids += (ydids.length() == 0 ? "" : ",") + info.getCid();

    ArrayList<QuestionInfo> result = new ArrayList<QuestionInfo>();
    if (ydids.length() == 0) return result;

    String sql = "SELECT * FROM question WHERE cid IN (" + ydids + ")";
    ResultSet rs = con.createStatement().executeQuery(sql);
    while (rs.next()) result.add(rs2Info(rs));
    return result;
  }

  private static QuestionInfo rs2Info(ResultSet rs) throws SQLException {
    QuestionInfo info = new QuestionInfo();
    info.setQid(rs.getLong("qid"));
    info.setCid(rs.getInt("cid"));

    info.setTx(rs.getInt("tx"));
    info.setContent(rs.getString("content"));
    info.setAnswer(rs.getString("answer"));

    info.setImportant(rs.getInt("important"));
    info.setComplexity(rs.getInt("complexity"));
    info.setType(rs.getInt("type"));

    info.setPickTime(rs.getLong("pickTime"));
    info.setRightTime(rs.getLong("rightTime"));
    return info;
  }
}
